package org.tong.activiti6.listeners;

import org.activiti.engine.delegate.event.ActivitiEntityEvent;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @date 2022-07-28 19:17
 */
@Component
public class MyActivitiEventListener implements ActivitiEventListener {
    @Override
    public void onEvent(ActivitiEvent event) {
        if (ActivitiEventType.TASK_CREATED == event.getType()) {
            ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;
            TaskEntity taskEntity = (TaskEntity) entityEvent.getEntity();
            Map<String, Object> map = new HashMap<>();
            map.put("taskId", taskEntity.getId());
            map.put("taskKey", taskEntity.getTaskDefinitionKey());
            map.put("tenantId", taskEntity.getTenantId());
            map.put("taskName", taskEntity.getName());
            System.out.println(map);

//            String taskDefinitionKey = taskEntity.getName();
//            if (taskDefinitionKey.endsWith("_callTask")) {
//                int i = 1 / 0;
//            }
        }
    }

    @Override
    public boolean isFailOnException() {
        return true;
    }
}
